package com.cs.common.http;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpRequestUtil {
    private static String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36";

    public static String doGet (String url) throws ClientProtocolException, IOException {
        String result = null;
        HttpClientBuilder hcBuilder = HttpClients.custom();
        hcBuilder.setUserAgent(userAgent);
        CloseableHttpClient httpclient = hcBuilder.build();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpclient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        if(entity != null){
            result = EntityUtils.toString(entity,"UTF-8");
        }
        return result;
    }

    public static String doPost(String url, String outStr) throws ClientProtocolException, IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpost = new HttpPost(url);
        httpost.setEntity(new StringEntity(outStr,"UTF-8"));
        HttpResponse response = httpclient.execute(httpost);
        String result = EntityUtils.toString(response.getEntity(),"UTF-8");
        return result;
    }

    public static String doPost(String url, Map<String, String> params) throws ClientProtocolException, IOException {
        String result = "";
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        List<NameValuePair> formparams = new ArrayList<>();
        for (String key : params.keySet()) {
            formparams.add(new BasicNameValuePair(key, params.get(key).toString()));
        }
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            CloseableHttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static InputStream doGetStream (String url) throws ClientProtocolException, IOException {
        InputStream inputStream = null;
        HttpClientBuilder hcBuilder = HttpClients.custom();
        hcBuilder.setUserAgent(userAgent);
        CloseableHttpClient httpclient = hcBuilder.build();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpclient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        if(entity != null){
            inputStream = entity.getContent();
        }
        return inputStream;
    }
}
